#! /usr/bin/env python3
# coding: utf-8
# date: 2023-06-10 1:20
# description: 自定义类（继承UserDict并读取设置键值）


from collections import UserDict


class A(UserDict):
    _sex = 'M'

    def __init__(self, *args, **kwargs):
        self.data = {}
        self.args = args
        self.kwargs = kwargs

    @property
    def sex(self):
        return self._sex

    @sex.setter
    def sex(self, value):
        if value not in 'MF':
            raise ValueError(f"{value}无效")
        self._sex = value

    def __getitem__(self, key):
        if key in self.data:
            return self.data[key]
        else:
            raise KeyError(key)

    def __setitem__(self, key, value):
        self.data[key] = value


a = A(1, 2, 3, name="admin", age=27)
print(getattr(a, 'kwargs'))
a['name'] = 'maxiao'
print(a['name'])
print(a.sex)
a.sex = 'F'
print(a.sex)
